package my.provider.demo.service;

import my.provider.demo.vo.User;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ServiceDefault {
    //key:userId
    private static ConcurrentHashMap<String, User> defaultMap = new ConcurrentHashMap<>(16);

    public String addUser(User user) {
        String userId = UUID.randomUUID().toString().replaceAll("-", "");
        user.setUserId(userId);
        defaultMap.put(userId, user);
        return userId;
    }

    public boolean updateUser(User user) {
        if (defaultMap.containsKey(user.getUserId())) {
            defaultMap.put(user.getUserId(), user);
            return true;
        }
        return false;
    }

    public User getUser(String userId) {
        return defaultMap.get(userId);
    }

    public boolean removeUser(String userId) {
        User user = defaultMap.remove(userId);
        return user == null ? false : true;
    }
}
